package cn.com.pc.penguin.controllers.admin;


import cn.com.pc.penguin.builder.keyword.Keyword;
import cn.com.pc.penguin.builder.keyword.KeywordDocumentBuilder;
import cn.com.pc.penguin.service.DocumentBuilder;
import cn.com.pc.penguin.service.PenguinService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

@Controller
@RequestMapping("/index")
public class PenguinIndexController {
    @Autowired
    private PenguinService penguinService;

    @RequestMapping("/all")
    @ResponseBody
    public String indexAll(@RequestParam String filePath) {
        try {
            long a = System.currentTimeMillis();
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GBK"));        //E:\\project\\penguin\\data\\auto_complete_material.txt
            String line;
            line = reader.readLine();
            int lineNo = 1;
            List<Keyword> keywords = new ArrayList<Keyword>();
            while (line != null) {
               // StringTokenizer token = new StringTokenizer(line, "|");
                Keyword keyword = new Keyword();
                keyword.setId((long) lineNo);
                keyword.setKeyword(line);
                keyword.setCreateDate(new Date());
                keyword.setResult(0);
                keyword.setSearches(0);
                keywords.add(keyword);
                if (lineNo % 1000 == 0) {
                    System.out.println(lineNo);
                    DocumentBuilder builder = new KeywordDocumentBuilder(keywords);
                    penguinService.index(builder);
                    keywords.clear();
                    Thread.sleep(5000);
                }
                line = reader.readLine();
                lineNo++;
            }
            if (keywords.size() > 0) {
                DocumentBuilder builder = new KeywordDocumentBuilder(keywords);
                penguinService.index(builder);
                keywords.clear();
            }
            reader.close();
            long b = System.currentTimeMillis();
            System.out.println("index success took : " + (b - a) + "ms");
            return String.valueOf(b-a);
        } catch (Throwable e) {
            e.printStackTrace();
            return "err";
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public String indexKeyword(@RequestParam String keyword) {
        try {
            List<Keyword> keywords = new ArrayList<Keyword>();
            Keyword kw = new Keyword();
            kw.setKeyword(keyword);
            kw.setCreateDate(new Date());
            kw.setResult(0);
            kw.setSearches(0);
            keywords.add(kw);
            DocumentBuilder builder = new KeywordDocumentBuilder(keywords);
            penguinService.index(builder);
            saveFile(keyword);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "err";
    }

    public void saveFile(String keyword) throws Exception{
        FileWriter fw = new FileWriter("E:\\project\\penguin\\data\\auto_complete_custom.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.newLine();
        bw.write(keyword);
        bw.close();
    }
}
